package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Attendance;
import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.service.AttendanceService;
import com.fan.teamer.service.EventService;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamService;

public class EventDeleteProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("EventDeleteProcess");
	private Team team;
	private Event event;
	private Member operator;
	private EventService eventService;
	private TeamService teamService;
	private MemberService memberService;
	private AttendanceService attendanceService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		EventDeleteProcess.logger = logger;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public AttendanceService getAttendanceService() {
		return attendanceService;
	}

	public void setAttendanceService(AttendanceService attendanceService) {
		this.attendanceService = attendanceService;
	}

	public int initCheck() {

		if (teamService == null || eventService == null || teamService == null
				|| attendanceService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (team == null || operator == null || event == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!eventService.hasEvent(event.getId())) {
			setErrorMessage("Event: " + event.getEventName()
					+ " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasEvent(event.getId())) {
			setErrorMessage("Team " + team.getName() + " doesn't have Event "
					+ event + " !");
			return TeamerGlobalVariables.FAILED;
		}

		if (!memberService.hasMember(operator.getEmail())) {
			setErrorMessage("Member " + operator.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		if (memberService.getMemberRoleInTeam(operator, team).equals(
				TeamerGlobalVariables.ORGANIZER)
				|| memberService.getMemberRoleInTeam(operator, team).equals(
						TeamerGlobalVariables.CO_ORGANIZER)
				|| event.getInitiator().equals(operator.getEmail())) {
			try {
				// attendance
				for (Attendance attendance : attendanceService
						.getAttendancesByTeamEvents(team, event)) {
					attendanceService.deleteAttendance(attendance);
				}
				eventService.deleteEvent(event);

			} catch (Exception e) {
				e.printStackTrace();
				setErrorMessage("Event " + event.getEventName() + " in team "
						+ team.getName() + " cannot be deleted");
				return TeamerGlobalVariables.FAILED;
			}
		} else {
			setErrorMessage("Member " + operator.getEmail()
					+ " doesn't have the right to remove event!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
